/**
 * 
 */
package edu.umd.clip.lm.tools;

import edu.umd.clip.lm.factors.*;
import edu.umd.clip.lm.factors.Dictionary;
import edu.umd.clip.lm.model.*;
import edu.berkeley.nlp.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class PrintVocabulary {

	public static class Options {
        @Option(name = "-input", required = false, usage = "Training data file (Default: stdin)")
		public String input;
        @Option(name = "-config", required = true, usage = "XML config file")
		public String config;
        @Option(name = "-factor", usage = "the factor (default: W)")
		public String factor = "W";
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
        OptionParser optParser = new OptionParser(Options.class);
        Options opts = (Options) optParser.parse(args, true);

		Experiment.initialize(opts.config);
		Experiment experiment = Experiment.getInstance();
		FactorTupleDescription desc = experiment.getTupleDescription();
		byte factorIdx = desc.getFactorIndex(opts.factor);
		if (factorIdx < 0) {
			System.err.printf("Factor %s not found.\n", opts.factor);
			return;
		}
		FactorDescription factorDesc = desc.getDescription(factorIdx);
		
		if (factorDesc.getParent() == null) {
			Dictionary dict = factorDesc.getDictionary();
			for(DictionaryIterator it = dict.iterator(false); it.hasNext(); ) {
				int word = it.next();
				System.out.println(dict.getWord(word));
			}
		} else {
			FactorDescription parentDesc = factorDesc.getParent();
			Dictionary parentDict = parentDesc.getDictionary();
			for(DictionaryIterator parentIt = parentDict.iterator(false); parentIt.hasNext(); ) {
				int parentWord = parentIt.next();
				Dictionary dict = factorDesc.getDictionary(parentWord);
				for(DictionaryIterator it = dict.iterator(false); it.hasNext(); ) {
					int word = it.next();
					System.out.println(dict.getWord(word));
				}
			}
		}
	}
}
